# -*- coding: utf-8 -*-
"""
Module Description:推送相关逻辑
Date: 2015/7/13
Author:Bai Jin Ping
"""
import pyshare.utils.json_utils as json
from pyshare.remote_services.push_service import PushService
from common import service_config
from pyshare.bll.decorators import catch_err
from pyshare.framework.async_utils.async_pool import async_pool


class PushId(object):
    """
    推送消息接口
    """
    ################
    # 保留接口(<100)-业务不能使用
    ################
    # 游戏内脱机挂检测验证码推送
    ROBOT_CAPTCHA = 11
    # 游戏内脱机挂检测验证码结果推送
    ROBOT_CAPTCHA_RESULT = 12

    ################
    # 联姻模块
    ################
    PRINCE_MARRY_LETTER = 102

    ################
    # 称号模块
    ################
    ROLE_TITLE = 201

    ################
    # 系统消息模块
    ################
    # 聊天消息推送
    CHAT_MSG = 301
    DEL_MSG = 302
    # 红包消息推送
    CHAT_RED_MSG = 303

    ################
    # 角色管理模块
    ################
    # 静默添加物品
    ADD_GOODS_SILENT = 401
    DEL_GOODS_SILENT = 402

    ###############
    # 博饼活动
    ##############
    MOON_ACTIVITY_MARQUEE = 510

    ################
    # 邮件模块
    ################
    # 新邮件提醒
    MAIL_NOTIFICATION = 701

    ################
    # 时装模块
    ################
    CLOTHES_CHANGE = 801
    HEAD_ID_CHANGE = 802
    ADD_CLOTH = 803     # 获得时装（包含时效）

    ################
    # 头像模块
    ################
    SMALL_HEAD_ID_CHANGE = 805
    NOW_SMALL_HEAD_ID = 806

    ################
    # 后宫模块
    ################
    # 封后推送
    QUEEN = 810

    ################
    # 道具模块
    ################
    ENTITY_ITEM = 888
    GIFT_ITEM = 889

    ###############
    # 校场来访
    ##############
    BATTLE_HISTORY = 901

    # ############
    # 联盟战
    # ###########
    # 攻城战血量
    SIEGE_WALL_HP = 1001
    # 攻城战跑马灯
    SIEGE_NOTIFICATION = 1002

    # ############
    # 联盟练兵帮助
    # ###########
    TRAIN_HELP = 1010

    # ############
    # 赴宴消息
    # ###########
    Dinner_Having = 1020

    # ############
    # 友邦系统
    # ############
    APPLY_FRIEND = 1101
    AGREE_APPLY = 1102
    REMOVE_FRIEND = 1103
    VISIT_FRIEND = 1104
    # 兼容
    APPLY_OF_FRIEND = 1201
    AGREE_APPLY_OF_FRIEND = 1202
    REMOVE_OF_FRIEND = 1203
    VISIT_OF_FRIEND = 1204

    # #########################
    # 跨服功臣战大臣血量推送
    # #########################
    CROSS_SIEGE_MINISTER_HP = 1301
    CROSS_SIEGE_BATTLE_MINISTER_INFO = 1302
    CROSS_SIEGE_BATTLE_CITY_INFO = 1303
    CROSS_SIEGE_AOE_INFO = 1304
    CROSS_SIEGE_BRIGAND_INFO = 1305

    # #########################
    # 跨服攻城战宝箱推送
    # #########################
    CROSS_SIEGE_CHEST = 1310
    CROSS_SIEGE_BREAK_CHEST = 1311  # 破城宝箱

    # #########################
    # 跨服遗迹争霸推送
    # #########################
    CROSS_SPECIAL_CHEST = 1401
    CROSS_RUINS_BATTLE = 1402
    CROSS_RUINS_BATTLE_WIN = 1403

    # 对联活动
    COUPLET_ACTIVITY = 1501

    # #########################
    # 跨服棋局队伍解散推送
    # #########################
    CROSS_CHESS_BREAK_TEAM = 1601
    CROSS_CHESS_BATTLE = 1602
    CROSS_CHESS_TASK = 1603
    CROSS_CHESS_CHEST = 1604
    CROSS_CHESS_TEAM_MEMBER = 1605

    # 遗迹夺宝
    RUINS_BATTLE = 1701

    # 扭蛋活动
    TWISTED_EGG = 1801

    # 跨服足球活动
    CROSS_SOCCER_RED_BOX = 1901     # 足球红包

    # 客服消息
    CUSTOMER_SERVICE_RED_DOT = 2000  # 客服消息红点

    # 交互道具受赠推送
    InterAction = 2020

    # #############那达慕大会推送#############
    # 生成boss推送
    CROSS_NAADAM_BOSS_CREATE = 2001
    # 秘境开启推送
    CROSS_NAADAM_SECRET_CREATE = 2002
    # 攻击秘境守护者推送
    CROSS_NAADAM_SECRET_BATTLE = 2003
    # 宝箱推送(发宝箱)
    CROSS_NAADAM_SECRET_SEND_RED = 2004
    # 宝箱推送(领取宝箱)
    CROSS_NAADAM_SECRET_RECEIVE_RED = 2005
    # 秘境大臣伤害榜推送
    CROSS_NAADAM_SECRET_MINISTER_RANK = 2006
    # 攻击boss推送
    CROSS_NAADAM_BOSS_BATTLE = 2007
    # 那达慕大会联盟变更推送
    CROSS_NAADAM_ALLIANCE_CHANGE = 2008
    # 凌烟阁
    COSTUME_SHOP_REFRESH = 2011  # 凌烟阁商品刷新

    # #############百家争鸣推送#############
    # 战败推送防守失败玩家
    CROSS_CONTEND_ACADEMY_FIGHT = 2201

    # #############圣君登基推送#############
    # 召开登基推送
    CROSS_SAINT_ASCEND_HOLD = 2101
    # 赐号推送
    CROSS_SAINT_REIGN = 2102
    # 开始登基推送
    CROSS_SAINT_ASCEND_START = 2103
    # 藏品获得推送
    UPDATE_ANTIQUE = 2104
    # 登基膜拜推送
    CROSS_SAINT_ASCEND_WORSHIP = 2105

    # #############同盟国消息推送#############
    # 加入同盟国
    CROSS_ALLIES_ENTER_COUNTRY = 2201
    CROSS_ALLIES_RERUIT_MSG = 2202
    CROSS_ALLIES_KICK_OUT = 2203
    CROSS_ALLIES_COPY_DAILY_IN = 2204
    CROSS_ALLIES_COPY_DAILY_OUT = 2205
    CROSS_ALLIES_COPY_DAILY_BREAK = 2206

    # #############跨服抽奖系统推送#############
    # 抽奖系统保底奖励
    CROSS_LOTTERY_MINI_AWARD = 2301

    # #############跨服蓬莱仙岛推送#############
    CROSS_ISLAND_BOSS_APPEAR = 2401         # 生成boss
    CROSS_ISLAND_UPGRADE_SCENE = 2405       # 升级场景推送
    CROSS_ISLAND_FIGHT = 2406               # 战斗击杀推送
    CROSS_ISLAND_UPDATE_HP = 2407           # 更新血量
    CROSS_ISLAND_COMPASS = 2408             # 罗盘搜索
    CROSS_ISLAND_BOSS_ATK = 2409            # 攻击boss
    CROSS_ISLAND_ASSIST_KILL_RED = 2410     # 协助击杀红点推送
    CROSS_ISLAND_CAN_REDEMPTION_RED = 2411  # 协助可迎回红点推送
    CROSS_ISLAND_RED_MARK = 2412            # 盟友标记红点推送
    CROSS_ISLAND_RED_REDEEM = 2413          # 请求盟友协助赎回大臣红点推送

    # #############九州之巅系统推送#############
    # 稀有奖励
    CROSS_SEASON_RARE_AWARD = 2501

    ################
    # 外显模块
    ################
    SHOW_CHANGE = 2601
    SHOW_ID_CHANGE = 2602

    # 当前赛季登录推送特权
    CROSS_SEASON_PUSH_RANK = 2701
    CROSS_SEASON_PAGEANT_INVITE = 2702      # 赛季盛会邀请
    CROSS_SEASON_PAGEANT_ATTEND = 2703      # 赛季盛会赴宴

    # 庆典发送礼花推送
    CROSS_SEASON_CELEBRATION_PUSH = 2801

    # #############跨服团购推送#############
    CROSS_GROUP_PURCHASE_PUSH = 2901

    # #############跨服三消推送#############
    CROSS_CLEARANCE_GIVE_ITEM = 3001         # 三消赠送道具推送

    # #############跨服大富翁推送#############
    CROSS_MONOPOLY_MATERIAL = 3101  # 大富翁原料制作完成推送
    CROSS_MONOPOLY_TRAP = 3102  # 大富翁陷阱脱困推送
    CROSS_MONOPOLY_INVITATION = 3103  # 大富翁秘境邀请推送
    CROSS_MONOPOLY_VIT_AWARD = 3104   # 大富翁购买联盟体力推送
    CROSS_MONOPOLY_BUY_GIFT_GRID = 3105    # 大富翁购买礼包格现金礼包推送
    CROSS_MONOPOLY_THANK_PRESENT = 3106  # 大富翁感谢联盟体力赠送者推送
    CROSS_MONOPOLY_ASSIST_NPC = 3107  # 大富翁击败标记npc
    CROSS_MONOPOLY_BUILDING_MAX_LV = 3108  # 大富翁建筑满级
    CROSS_MONOPOLY_COMPLETE_ALLIANCE_BUFF = 3109  # 大富翁完成联盟Buff
    CROSS_MONOPOLY_ASSIST_RED = 3110  # 求助区红点推送

    # #############端午抽奖推送#############
    Dragon_Boat_Lottery = 3201  # 需要推送的奖励推送

    # #############跨服攻城掠地推送#############
    # 地块拥有者实时变更(标签)
    CROSS_SIEGELORD_OWNER_CHANGE = 3301                 # 地块拥有者变更推送
    CROSS_SIEGELORD_DEF_TEAM_INFO = 3302               # 地块防守队伍数量推送
    # 流水(标签)
    CROSS_SIEGELORD_ATK_TEAM_DEATH = 3303               # 进攻队伍被击溃推送
    CROSS_SIEGELORD_RESOURCES_OWNER_CHANGE = 3304       # 资源地块拥有者变更推送
    # 弹窗(个人)
    CROSS_SIEGELORD_TRIGGER_CASH_GIFT = 3305            # 触发限时现金礼包推送
    CROSS_SIEGELORD_SHARE_COUNTRY_VIT = 3306            # 分享盟友体力
    CROSS_SIEGELORD_ATK_TEAM_BE_ATTACK = 3307           # 进攻队伍被攻击

    # #############青瓷御器推送#############
    CROSS_CELADON_VIT_AWARD = 3401      # 青瓷御器购买全盟礼包体力推送
    CROSS_CELADON_BUY_ALLIES_GIFT = 3402  # 青瓷御器购买全盟礼包公告推送
    CROSS_CELADON_THANK_PRESENT_VIT = 3403  # 青瓷御器感谢全盟礼包体力赠送者推送
    CROSS_CELADON_ASSIST_AWARD = 3404     # 青瓷御器可领取协助奖励推送（Npc标记与互助宝箱）
    CROSS_CELADON_ASSIST_RED = 3405     # 求助区红点推送
    CROSS_CELADON_THANK_PRESENT_AWARD = 3406  # 青瓷御器感谢藏宝图赠礼推送
    CROSS_CELADON_PRESENT_AWARD = 3407  # 青瓷御器藏宝图赠礼推送
    CROSS_CELADON_PVP_FIGHT = 3408      # 青瓷御器被攻击推送
    CROSS_CELADON_BATTLE_AWARD_COUNT = 3409    # 青瓷御器使用者盟战令奖励推送
    CROSS_CELADON_ALLIES_BATTLE = 3410     # 开启盟战令推送
    CROSS_CELADON_BATTLE = 3411    # 青瓷御器战斗公告
    CROSS_CELADON_DECLARE = 3412  # 被宣战推送

    # ############拼图推送##############
    PUZZLE_ASK_FRAGMENT = 3501      # 索取碎片推送
    PUZZLE_SEND_FRAGMENT = 3502     # 赠送碎片推送

    # #############皇决之选--推送#############
    CROSS_FIGHT_USE_VOTE = 3601                 # 皇决之选--使用投票券推送
    CROSS_FIGHT_WIN = 3602                      # 皇决之选--战胜对手
    CROSS_FIGHT_STREAK_WIN_TIMES = 3603         # 皇决之选--连胜次数
    CROSS_FIGHT_WIN_TOP_TEN = 3604              # 皇决之选--战胜前十玩家
    CROSS_FIGHT_REVOLT_WIN = 3605               # 皇决之选--反击令战胜
    CROSS_FIGHT_PERFECT_WIN = 3606              # 皇决之选--全歼对手

    # #############推送礼包推送#############
    PUSH_GIFT_ADD_CUSTOM_ROLE = 3701            # 自定义礼包角色推送

    # #############爬塔推送#############
    CROSS_TOWER_ASSIST_RED = 3801  # 求助区红点
    CROSS_TOWER_ASSIST_AWARD = 3802  # 可领取协助奖励
    CROSS_TOWER_BATTLE = 3803  # 战斗公告
    CROSS_TOWER_TRAP = 3804  # 陷阱脱困推送
    CROSS_TOWER_TRIGGER_GIFT = 3805  # 触发限时现金礼包推送
    CROSS_TOWER_UPGRADE_FLOOR = 3806  # 进入下一层推送
    CROSS_TOWER_UPGRADE_REGION = 3807  # 进入区域推送
    CROSS_TOWER_HONOR = 3808  # 荣誉推送
    CROSS_TOWER_BOSS_ASSIST = 3809  # Boss协助

    # #############御海垂纶#############
    CROSS_FISH_ALLIES_ASSIST_AWARD_RED = 3901   # 可领取协助奖励红点


@catch_err
def call_push_data(push_id, data, send_rid_list=None, server_id=None):
    """
    调用push服务, 推送给指定的多个玩家
    """
    server_id = server_id if server_id else service_config.get_server_id()
    app_id = service_config.get_app_id()

    if not send_rid_list:
        return

    if not isinstance(data, basestring):
        data = json.dumps(data)

    # 异步线程调用推送
    async_pool.submit(PushService.push_data,
                      msg_type=push_id,
                      msg_data=data,
                      rid_list=send_rid_list,
                      srv_id=server_id,
                      app_id=app_id)


@catch_err
def call_push_data_to_all(push_id, data, server_id=None):
    """
    调用push服务, 推送给某个区服的所有玩家
    """
    server_id = server_id if server_id else service_config.get_server_id()
    app_id = service_config.get_app_id()

    if not isinstance(data, basestring):
        data = json.dumps(data)

    # 异步线程调用推送
    async_pool.submit(PushService.push_to_all,
                      msg_type=push_id,
                      msg_data=data,
                      srv_id=server_id,
                      app_id=app_id)


@catch_err
def call_push_data_to_server_id_list(push_id, data, server_id_list):
    """
    调用push服务, 推送给多个区服的所有玩家
    """
    server_id = service_config.get_server_id()
    app_id = service_config.get_app_id()

    if not isinstance(data, basestring):
        data = json.dumps(data)

    # 异步线程调用推送
    async_pool.submit(PushService.push_to_server_id_list,
                      msg_type=push_id,
                      msg_data=data,
                      srv_id=server_id,
                      app_id=app_id,
                      server_id_list=server_id_list
                      )


@catch_err
def call_push_data_to_label(label, push_id, data):
    """
    调用push服务, 推送到app_id的某个label, 有注册接收该label消息的客户端都能收到
    """
    app_id = service_config.get_app_id()

    if not isinstance(data, basestring):
        data = json.dumps(data)

    # 异步线程调用推送
    async_pool.submit(PushService.push_to_label,
                      label=label,
                      msg_type=push_id,
                      msg_data=data,
                      app_id=app_id)
